var tools = {};
/*
	Cookie工具
	使用方法:
		//存值
		var value = "7天";
		tools.cookie("day",value, {expires:7});	//将字符串:"7天" 以 "day"这个key保存到cookie中5天
		//取值
		var v = tools.cookie("day");			//用 "day" 这个key从cookie取出值
*/
tools.cookie = function(name, value, options) {
	if (typeof value != 'undefined') { // name and value given, set cookie
		options = options || {};
		if (value === null) {
			value = '';
			options.expires = -1;
		}
		var expires = '';
		if (options.expires
				&& (typeof options.expires == 'number' || options.expires.toGMTString)) {
			var date;
			if (typeof options.expires == 'number') {
				date = new Date();
				date.setTime(date.getTime()
						+ (options.expires * 24 * 60 * 60 * 1000));
			} else {
				date = options.expires;
			}
			expires = '; expires=' + date.toGMTString(); // use expires
			// attribute,
			// max-age is not
			// supported by IE
		}
		var path = options.path ? '; path=' + options.path : '';
		var domain = options.domain ? '; domain=' + options.domain : '';
		var secure = options.secure ? '; secure' : '';
		document.cookie = [ name, '=', encodeURIComponent(value), expires,
				path, domain, secure ].join('');
	} else { // only name given, get cookie
		var cookieValue = null;
		if (document.cookie && document.cookie != '') {
			var cookies = document.cookie.split(';');
			for ( var i = 0; i < cookies.length; i++) {
				var cookie = jQuery.trim(cookies[i]);
				// Does this cookie string begin with the name we want?
				if (cookie.substring(0, name.length + 1) == (name + '=')) {
					cookieValue = decodeURIComponent(cookie
							.substring(name.length + 1));
					break;
				}
			}
		}
		return cookieValue;
	}
};
/*
	将json对象转换为字符串
	使用方法:
	var json = {id=3,name="abc"};
	var str = tools.j2s(json);
 */
tools.j2s = function(O) {
	var S = [];
	var J = "";
	if (Object.prototype.toString.apply(O) === '[object Array]') {
		for ( var i = 0; i < O.length; i++) {
			S.push(this.j2s(O[i]));
		}
		J = '[' + S.join(',') + ']';
	} else if (Object.prototype.toString.apply(O) === '[object Date]') {
		J = "new Date(" + O.getTime() + ")";
	} else if (Object.prototype.toString.apply(O) === '[object RegExp]' || Object.prototype.toString.apply(O) === '[object Function]') {
		J = O.toString();
	} else if (Object.prototype.toString.apply(O) === '[object Object]') {
		for ( var i in O) {
			var tempObj = "";
			if(typeof (O[i]) == 'string') {
				tempObj = '"' + O[i] + '"';
			} else if(typeof (O[i]) === 'object') {
				tempObj = this.j2s(O[i]);
			} else {
				tempObj = O[i];
			}
			S.push(i + ':' + tempObj);
		}
		J = '{' + S.join(',') + '}';
	} else {
		J = O;
	}
	return J;
};
//json字符串转换为json对象
tools.s2j = function(jsonString) {
	return eval('('+jsonString+')');
};
//json转提交数据
tools.json2Data = function(attrName, obj) {
	var postDate = {};
	for(var attr in obj) {
		if($.isArray(obj[attr])) {
			var objList = obj[attr];
			for(var i=0;i<objList.length;i++) {
				$.extend(postDate, tools.json2Data(attrName + "." + attr + "[" + i + "]", objList[i]));
			}
		} else {
			postDate[attrName + "." + attr] = obj[attr];
		}
	}
	return postDate;
};
//生成随机数
tools.rndNum = function(Min, Max){
	var Range = Max - Min; 
    var Rand = Math.random(); 
    return(Min + Math.round(Rand * Range)); 
};

tools.toHtml = function(str){
	str="<p>" + str + "</p>";
	str=str.replace(/\r\n/ig,"</p><p>"); 
	return str; 
};

//页面跳转
tools.href = function(url) {
	if(url == null) {
		location.reload();
	} else {
		window.location.href = ctx + url;
	}
}


/**
 * 空文本框内部提示文字效果
 */
$.fn.prompt = function(attr) {
	if("undefined" == typeof attr) attr = "defaultValue";
	$(this).each(function() {
		if($(this).val() == "" || $(this).val() == $(this).attr(attr)) {
			if($(this).is(":password")) {
				$("<div>" + $(this).attr(attr) + "</div>")
				.addClass($(this).attr("class"))
				.css({position:"absolute", color:"#999999", top:0}).click(function() {
					$(this).hide().siblings().focus();
				})
				.insertAfter($(this))
				.parent()
				.css({position:"relative"});
			} else {
				$(this).val($(this).attr(attr)).css("color", "#999999");
			}
		} else {
			$(this).css("color", "#000000");
		}
		$(this).focus(function() {
			if($(this).is(":password")) {
				$(this).siblings("div").hide();
			}
			if($(this).val() == $(this).attr(attr) || $(this).val() == "") {
				$(this).val("").css("color", "#000000");
			}
		}).focusout(function() {
			if($(this).val() == "") {
				if($(this).is(":password")) {
					$(this).siblings("div").show();
				} else {
					$(this).val($(this).attr(attr)).css("color", "#999999");
				}
			}
		});
	});
};
$.fn.imgAuto = function(dWidth, dHeight) {
	
	$(this).each(function() {
		var imgWidth = $(this).width();
		var imgHeight = $(this).height();
		
		if($(this).parent().is("[class!='autoImg']")){
			var $div = $("<div></div>").addClass($(this).attr("class")).addClass("autoImg").css({overflow:"hidden", display:"display"});
			if(imgWidth > 0 ) {
				$div.width(imgWidth);
			}
			if(imgHeight > 0 ) {
				$div.height(imgHeight);
			}
			if(dWidth && dHeight) {
				$div.width(dWidth).height(dHeight);
			}
			$(this).wrap($div);
		}
		
		$(this).css({position:"relative"}).load(function() {
			var $imgObj = $(this).removeAttr("width").removeAttr("height").removeAttr("class");
			$imgObj.attr("defaultWidth", $imgObj.get(0).clientWidth).attr("defaultHeight", $imgObj.get(0).clientHeight);

			var pWidth = $imgObj.parent().width();
			var pHeight = $imgObj.parent().height();

			if(($imgObj.attr("defaultWidth") - pWidth) < 0 && ($imgObj.attr("defaultHeight") - pHeight) < 0){
				$imgObj.removeAttr("width");
				$imgObj.removeAttr("height");
			} else if($imgObj.attr("defaultWidth") - pWidth < $imgObj.attr("defaultHeight") - pHeight) {
				$imgObj.attr("height", pHeight);
				$imgObj.removeAttr("width");
			} else if($imgObj.attr("defaultWidth") - pWidth > $imgObj.attr("defaultHeight") - pHeight){
				$imgObj.attr("width", pWidth);
				$imgObj.removeAttr("height");
			}
			$imgObj.css("left", (pWidth - $imgObj.get(0).clientWidth)/2 + "px").css("top", (pHeight - $imgObj.get(0).clientHeight)/2 + "px");
		});
	});
}